package com.flymobi.service;

import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Ad;
import com.flymobi.util.QL;
import com.flymobi.vo.NewsSearchVo;

public class AdService extends CommonService {

	public List<Ad> list(int startRow, int pagePerSize) {
		return dao.find(Ad.class, QL.HQL.Ad.LIST, new Object[] {},startRow, pagePerSize);
	}
	
	public List<Ad> listOn(int startRow, int pagePerSize) {
		return dao.find(Ad.class, QL.HQL.Ad.LIST_ON, new Object[] {},startRow, pagePerSize);
	}
	
	public List<Ad> list(NewsSearchVo param,int startRow, int pagePerSize) {
		String hql="from Ad m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(!StringUtil.isEmpty(param.getKey())&&param.getKey().trim().length()>0){
				hql+=" and m.adOwner like ?";
				paramList.add("%"+param.getKey()+"%");
			}
			if(param.getBlockid()!=null&&param.getBlockid()>0){
				hql+=" and m.id in(select n.contId from PageBlockContent n where n.pageBlockId=? and n.type='2')";
				paramList.add(param.getBlockid());
			}
		}
		hql+=" order by m.status desc,m.id desc";
		return dao.find(Ad.class,hql, paramList,startRow, pagePerSize);
	}
	
	@SuppressWarnings("unchecked")
	public  List<Ad>  listFromCache(int startRow, int pagePerSize) {
		List<Ad> list=dao.find(Ad.class, QL.HQL.Ad.LIST, new Object[] {},startRow, pagePerSize);
		return list;
	}
	
	public Long getCount() {
		return dao.getCount(QL.HQL.Ad.GET_COUNT, new Object[] {});
	}
	
	public Long getCountOn() {
		return dao.getCount(QL.HQL.Ad.GET_COUNT_ON, new Object[] {});
	}
	
	public Long getCount(NewsSearchVo param) {
		String hql="select count(*) from Ad m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?"; 
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(param.getKey()!=null&&param.getKey().trim().length()>0){
				hql+=" and m.adOwner like ?";
				paramList.add("%"+param.getKey()+"%");
			}
			if(param.getBlockid()!=null&&param.getBlockid()>0){
				hql+=" and m.id in(select n.contId from PageBlockContent n where n.pageBlockId=? and n.type='2')";
				paramList.add(param.getBlockid());
			}
		}
		return dao.getCount(hql, paramList);
	}

	public void save(Ad Ad) {
		dao.save(Ad);
	}

	public Ad load(String id) {
		return dao.load(Ad.class, new Long(id));
	}
	
	public Ad get(String id) {
		return dao.get(Ad.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
}
